package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao.teamDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.dao_impl.teamDaoImpl;
import com.example.yiwei.crm.entity.ClockEntity;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class ClockDetailedActivity extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private SharedPreferences sp;
    ImageView img_back;
    AutoListView lstv;
    List<ClockEntity>list;
    MyAdapter ladapter;
    List<ClockEntity> list1;
    int page=1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ClockEntity> result = (List<ClockEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    list.clear();
                    list.addAll(result);
                    break;
                case AutoListView.LOAD:
                    lstv.onLoadComplete();
                    if (result!=null){
                        list.addAll(result);
                    }

                    break;
            }
//            lstv.setResultSize(result.size());
            ladapter.notifyDataSetChanged();

        }

        ;
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock_detailed);
        img_back= (ImageView) findViewById(R.id.clock_detailed_btn_back);
        lstv= (AutoListView) findViewById(R.id.clock_detailed_lstv);
        lstv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lstv.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        lstv.setDividerHeight(1);
        lstv.setOnRefreshListener(ClockDetailedActivity.this);
        lstv.setOnLoadListener(ClockDetailedActivity.this);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        list1=new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                teamDao teamDao=new teamDaoImpl();
                SC sc=new SC();
                JSONObject object=sc.storage(sp);
                try {
                    object.put("UAB01",sp.getString("CID01",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json=object.toString();
                list=teamDao.queryClockMessage(baseClass.find_clock_record+1,json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list==null){
                            Toast.makeText(ClockDetailedActivity.this,"没有打卡记录", Toast.LENGTH_SHORT).show();
                        }else{
                            ladapter=new MyAdapter();
                            lstv.setAdapter(ladapter);
                            ladapter.notifyDataSetChanged();
                        }

                    }
                });

            }
        }).start();
    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
            LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            teamDao teamDao=new teamDaoImpl();
            SC sc=new SC();
            JSONObject object=sc.storage(sp);
            try {
                object.put("UAB01",sp.getString("CID01",null));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json=object.toString();
            list1=teamDao.queryClockMessage(baseClass.find_clock_record+1,json);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
           if (list!=null){
               if (lstv.getVisibility()==View.GONE){
                   lstv.setVisibility(View.VISIBLE);
               }
               msg.obj = list1;
               handler.sendMessage(msg);
           }else {
               lstv.setVisibility(View.GONE);
           }
            super.onPostExecute(integer);
        }
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            customerDao customerdao = new customerDaoImpl();
            page=page+1;
            teamDao teamDao=new teamDaoImpl();
            SC sc=new SC();
            JSONObject object=sc.storage(sp);
            try {
                object.put("UAB01",sp.getString("CID01",null));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json=object.toString();
            list1=teamDao.queryClockMessage(baseClass.find_clock_record+page,json);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (list1!=null){
                msg.obj = list1;
                handler.sendMessage(msg);
            }else {
                msg.obj = list1;
                handler.sendMessage(msg);
                Toast.makeText(ClockDetailedActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }

            super.onPostExecute(integer);
        }
    }
    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.clock_listview,null);
                holder = new ViewHolder();
                holder.txt_time= (TextView) convertView.findViewById(R.id.listview_clock_time);
                holder.txt_address= (TextView) convertView.findViewById(R.id.listview_clock_address);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txt_address.setText(list.get(position).getClock_address());
            if (list.get(position).getClock_time() != null) {
                boolean b = list.get(position).getClock_time().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
                if (b) {
                    holder.txt_time.setText(list.get(position).getClock_time());
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                    String next_time = list.get(position).getClock_time() ;
                    long l = Long.parseLong(next_time);
                    Date date = new Date(l);
                    String nTime = sdf.format(date);
                   holder.txt_time.setText(nTime);
                }
            }
            return convertView;
        }
        class ViewHolder {
            TextView txt_time,txt_address;
        }
    }
}
